package tpiClient;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import tpiprotocole.LH;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @date 13 avr. 2011
 * @time 13:53:58
 * @author Luis.HAFNER
 */
class ControleurClient implements ActionListener {

    private VueClient vueClient;
    private ModeleClient modeleClient;
    private PrintWriter emission =null;
    private Socket socket;
    private BufferedReader reception;

    public ControleurClient(VueClient vueClient, ModeleClient modeleClient) {
        this.vueClient = vueClient;
        this.modeleClient = modeleClient;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==this.vueClient.getBoutonConnecter() && this.vueClient.getBoutonConnecter().getText().equals("Connecter") && !this.vueClient.getChampNom().getText().isEmpty()){
            this.modeleClient.setNom(this.vueClient.getChampNom().getText());
            try {
                socket = new Socket(this.vueClient.getChampIP().getText(), Integer.parseInt(this.vueClient.getChampPort().getText()));
                // Lecture du socket
                reception = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                // Ecriture du socket
                emission = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                emission.println(LH.Connecter(this.modeleClient.getNom()));
                String reponse = reception.readLine();
                if (reponse != null && reponse.startsWith("LH")) {
                    // Supprimer la partie du protocole pour simplifier la chaine
                    reponse = reponse.substring(2);
                    if(reponse.startsWith("101")){
                        this.vueClient.messageBox("Connexion établie!");
                        this.vueClient.changerEtatBoutonDemarrer();
                        this.vueClient.changerEtatBoutonEnvoyer();
                    }
                    else if(reponse.startsWith("102")){
                        this.vueClient.messageBox("Erreur "+reponse+" : Le nom existe déjà. Changez le.");
                    }
                }

            } catch (UnknownHostException ex) {
                System.out.println(ex+"!");
            } catch (IOException ex) {
                this.vueClient.messageBox("Serveur non atteignable");
            }
        }
        else if((e.getSource()==this.vueClient.getBoutonConnecter() && this.vueClient.getBoutonConnecter().getText().equals("Déconnecter"))){
            try {
                emission.println(LH.Deconnecter());
                this.vueClient.changerEtatBoutonDemarrer();
                this.emission.close();
                this.reception.close();
                this.socket.close();
                this.vueClient.changerEtatBoutonEnvoyer();
            } catch (IOException ex) {
                System.out.println(ex);
            }

        }
        else if(e.getSource()==this.vueClient.getBoutonEnvoyer() && emission!=null){

            String texte = this.vueClient.getChampEnvoi().getText();
            if(!texte.isEmpty()){
                this.vueClient.getTexteEnvoye().append(texte+"\n");
                emission.println(LH.EnvoiTexte(texte));
                this.vueClient.getChampEnvoi().setText("");
            }
        }
        else if(e.getSource()==this.vueClient.getBoutonQuitter()){
            try{
                this.emission.close();
                this.reception.close();
                this.socket.close();
                System.exit(0);
            } catch (IOException ex) {
                System.out.println(ex);
            }

        }

    }
}
